﻿/*
Copyright © 2010 François Karman

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

See <license.txt> for the complete license of the software.
*/

using System;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.Build.Framework;
using MSBuild.Mercurial.Command;

namespace MSBuild.Mercurial
{
    /// <summary>
    /// Builds a command based on a specific MSBuild task.
    /// </summary>
    /// <seealso cref="ParameterAttribute" />
    internal static class CommandBuilder
    {
        /// <summary>
        /// Builds a command based on a specific MSBuild task.
        /// </summary>
        /// <param name="task">
        /// The MSBuild task used as a reference for the generation.
        /// </param>
        /// <returns>
        /// The generated Mercurial command parameters.
        /// </returns>
        public static string Build(AbstractHgTask task)
        {
            if (task == null)
            {
                throw new ArgumentNullException("task");
            }

            // The created command line
            StringBuilder commandLine = new StringBuilder();

            // Extract the Mercurial command associated to the task
            CommandAttribute command = task.GetType().GetCustomAttributes(typeof(CommandAttribute), true).First() as CommandAttribute;
            if (command == null)
            {
                string format = Error.TaskWithoutAttribute;
                string message = string.Format(Error.Culture, format, task.GetType().Name);
                throw new ArgumentException(message, "task");
            }
            else
            {
                commandLine.Append(command.Command);
            }

            // The value of the default parameter
            string defaultParameter = null;

            // Parse all the properties of the task
            foreach (PropertyInfo property in task.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty))
            {
                object[] attributes = property.GetCustomAttributes(typeof(ParameterAttribute), true);
                if (attributes != null && attributes.Length > 0)
                {
                    // Retrieve prefix and value
                    ParameterAttribute attribute = attributes[0] as ParameterAttribute;
                    Type propertyType = property.PropertyType;
                    object propertyValue = property.GetValue(task, null);
                    string parameterPrefix = attribute.Prefix;
                    string parameterValue = ParameterBuilderFactory.Instance.Build(parameterPrefix, propertyType, propertyValue);

                    if (!string.IsNullOrEmpty(parameterValue))
                    {
                        if (string.IsNullOrEmpty(attribute.Prefix))
                        {
                            // Default parameter
                            defaultParameter = parameterValue;
                        }
                        else
                        {
                            commandLine.Append(" ");
                            commandLine.Append(parameterValue);
                        }
                    }
                }
            }

            // Add the default parameter, if any
            if (!string.IsNullOrEmpty(defaultParameter))
            {
                commandLine.Append(" ");
                commandLine.Append(defaultParameter);
            }

            // Add the suffix, if any
            if (!string.IsNullOrEmpty(command.Suffix))
            {
                commandLine.Append(" ");
                commandLine.Append(command.Suffix);
            }

            return commandLine.ToString();
        }
    }
}
